package com.sugar.subscribe.course.module.business.dal.mysql.coachplan;

import java.util.*;

import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.sugar.subscribe.course.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.subscribe.course.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.sugar.subscribe.course.module.business.dal.dataobject.coach.CoachDO;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachplan.CoachPlanDO;
import com.sugar.subscribe.course.module.business.dal.dataobject.serviceclass.ServiceClassDO;
import org.apache.ibatis.annotations.Mapper;
import com.sugar.subscribe.course.module.business.controller.admin.coachplan.vo.*;

/**
 * 教练课程方案 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface CoachPlanMapper extends BaseMapperX<CoachPlanDO> {

    default PageResult<CoachPlanRespVO> selectPage(CoachPlanPageReqVO reqVO) {
        MPJLambdaWrapperX<CoachPlanDO> wrapper = new MPJLambdaWrapperX<CoachPlanDO>()
                .selectAll(CoachPlanDO.class)
                .selectAs("s1.name", CoachPlanRespVO::getServiceName)
                .selectAs("s2.name", CoachPlanRespVO::getServicePName)
                .selectAs(CoachDO::getName, CoachPlanRespVO::getCoachName)
                .leftJoin(CoachDO.class, CoachDO::getId, CoachPlanDO::getCoachId)
                .leftJoin(ServiceClassDO.class, "s1", ServiceClassDO::getId, CoachPlanDO::getServiceId)
                .leftJoin(ServiceClassDO.class, "s2", ServiceClassDO::getId, CoachPlanDO::getServicePId);
        Long[] serviceIds = reqVO.getServiceIds();
        if (serviceIds != null){
            switch (serviceIds.length){
                case 2:
                    wrapper.eq(CoachPlanDO::getServiceId, serviceIds[1]);
                case 1:
                    wrapper.eq(CoachPlanDO::getServicePId, serviceIds[0]);
            }
        }
        return selectJoinPage(reqVO, CoachPlanRespVO.class, wrapper
                .eqIfPresent(CoachPlanDO::getCoachId, reqVO.getCoachId())
                .eqIfPresent(CoachPlanDO::getAuditStatus, reqVO.getAuditStatus())
                .eqIfPresent(CoachPlanDO::getPeopleCount, reqVO.getPeopleCount())
                .betweenIfPresent(CoachPlanDO::getServiceTime, reqVO.getServiceTime())
                .eqIfPresent(CoachPlanDO::getPrice, reqVO.getPrice())
                .betweenIfPresent(CoachPlanDO::getCreateTime, reqVO.getCreateTime())
                .likeIfPresent(CoachDO::getName, reqVO.getCoachName())
                .orderByDesc(CoachPlanDO::getId));
    }

    default PageResult<CoachPlanDO> appSelectPage(PageParam pageParam, Long coachId){
        return selectPage(pageParam, new LambdaQueryWrapperX<CoachPlanDO>()
                .eq(CoachPlanDO::getCoachId, coachId)
                .orderByDesc(CoachPlanDO::getSort));
    }
}